/**
 * 
 */
package com.feib.stms.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import com.feib.stms.dao.BasePaymentFileContentDao;
import com.feib.stms.dao.DeliveryPaymentChkDao;
import com.feib.stms.dao.DeliveryPaymentDao;
import com.feib.stms.dao.PurchasePaymentDao;
import com.feib.stms.dao.PurchaseReturnPaymentDao;
import com.feib.stms.dao.jdbc.SqlQueryMapProvider;
import com.feib.stms.model.BasePaymentFileContent;
import com.feib.stms.pagination.PaginatedList;
import com.feib.stms.service.BasePaymentFileContentService;


/**
 * @author Jimmy Liu
 *
 */
public abstract class BasePaymentFileContentServiceImpl <K extends BasePaymentFileContentDao<T>, T extends BasePaymentFileContent > extends BaseServiceImpl implements BasePaymentFileContentService<K, T> {

	private static final long serialVersionUID = 2361491209592707874L;
	
	protected K dao;

	@Resource(name="dataSource")
	protected DataSource dataSource;

	@Resource(name="temporaryBaseDirectory")
	protected String temporaryBaseDirectory;

	public abstract void setDao(K dao) ;

	public PaginatedList<T> findPaginatedListByPaymentFileContent(T entity,
			Date deliveryDateFrom, Date deliveryDateTo,
			List<String> fileFormats, List<String> constrainSecuritiesNos,
			PaginatedList<T> paginatedList) 
	{
		return dao.queryForPaginatedListByPaymentFileContent(entity, deliveryDateFrom, deliveryDateTo, fileFormats, constrainSecuritiesNos, paginatedList);
	}
	
	public String exportExcelByPaymentFileContent(T entity, Date deliveryDateFrom, Date deliveryDateTo, List<String> fileFormats, List<String> constrainSecuritiesNos, PaginatedList<T> paginatedList) throws Exception 
	{
		String[] headers = new String[]{"帳號", "帳號檢查碼M", "客戶戶名", "淨收金額", "淨付金額"
										, "金額檢查碼N", "處理類別", "證券代號", "證券名稱", "扣款優先種類"
										, "扣款先後序號", "交割日期", "營業員", "狀態"};
		

		String sheetName = "";
		if (dao instanceof DeliveryPaymentChkDao) {
			sheetName = "驗資明細表";			
		}
		else if (dao instanceof DeliveryPaymentDao) {
			sheetName = "交割款明細表";			
		}
		else if (dao instanceof PurchasePaymentDao) {
			sheetName = "申購款明細表";			
		}
		else if (dao instanceof PurchaseReturnPaymentDao) {
			sheetName = "申購退款明細表";			
		}
		
		SqlQueryMapProvider sqlQueryMapProvider = this.dao.getSqlQueryMapProviderByPaymentFileContent(entity, deliveryDateFrom, deliveryDateTo, fileFormats, constrainSecuritiesNos, paginatedList);
		
		BasePaymentFileContentExcelExportor exportor = new BasePaymentFileContentExcelExportor(dataSource, sqlQueryMapProvider, headers, temporaryBaseDirectory, sheetName);
		
		String fileName = exportor.export();
		return fileName;
	}

}
